package ua.pp.shell.wka;

import java.util.Iterator;
import java.util.List;

import ua.pp.shell.wka.DBAdapter.Types.Word;
import android.content.Context;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import com.google.api.translate.Language;
import com.google.api.translate.Translate;

public class Translator {
	public static String referrer = "http://www.shell.pp.ua";

    public static String translate(String text, Language language_from, Language language_to) throws Exception {
        Translate.setHttpReferrer(referrer);
        String translatedText = Translate.execute(text, language_from, language_to);
        return translatedText;
    }
    
    public static void FillLanguageSpinner(Context context, Spinner spinner, String language) {
    	ArrayAdapter<String> languages = GetLanguageSpinnerAdapter(context);
        spinner.setAdapter(languages);
    }
    
    public static void FillAnswerSpinner(Context context, Spinner spinner, List<Word> words, Word word){
    	spinner.setPrompt(word.word);
    	ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item);
    	adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	Iterator<Word> iterator = words.iterator();
    	while(iterator.hasNext()) {
    	    Word i_word = (Word)iterator.next(); 
    	    adapter.add(i_word.translation);
    	} 
    	spinner.setAdapter(adapter);
    }
    
    public static ArrayAdapter<String> GetLanguageSpinnerAdapter(Context context) {
    	ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.simple_spinner_item);
    	adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    	for (Language l : Language.values()) {
    	    adapter.add(l.name());
    	}
    	return adapter;
    }
    
    public static Language GetLanguage(String language) throws Exception {
    	for (Language l : Language.values()) {
    		String ff = l.name();
            if (ff.equals(language)) {
                return l;
            }
        }
        throw new Exception("Unknown language pr ovided : " + language);
    }
}
	